import query from '@dfeidao/atom-nodejs/db/query';
// import format from '@dfeidao/atom-nodejs/dt/format';
// import query from '@dfeidao/atom-nodejs/db/query';
import { IncomingHttpHeaders } from 'http';
import { file_url, spaceid, systemid } from '../atom/config';

interface Message {
	filter: {
		teacher_no: string;
	};
	page_size: number;
	page_no: number;
	productid: string;
}

interface IWebResult {
	data: unknown;
	cookie?: {
		[name: string]: string;
	} | null;
	content_type?: string;
	headers?: {
		[key: string]: string[];
	};
	attachment?: string;
	redirect?: string;
	status_code?: number;
}

export default async function atom(msg: Message, action_id: string, session_id: string, headers: IncomingHttpHeaders): Promise<IWebResult> {

	const { prepare: p, exec: e } = await query(action_id, session_id, systemid);
	// 1.依据"教师编号" 查询<教师>表得到"头像",
	p('teacher', ['headimg', 'teacher_name'], { teacher_no: msg.filter.teacher_no, productid: msg.productid }, 200, msg.page_no, [], []);
	const [img] = await e<{ headimg: string, count: number, imaging: string }>();
	img[0].imaging = file_url + '/getfile?productid=' + spaceid + '&id=' + img[0].headimg;

	// 2.依据“教师编号” 查询<上课记录>表得到“总条数”
	p('class_record', ['record_no'], { teacher_no: msg.filter.teacher_no, productid: msg.productid }, 200, msg.page_no, [], []);
	const [classes] = await e<{ record_no: string }>();
	img[0].count = classes.length;


	return { data: img[0] } as IWebResult;
}
